﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace dodone
{
    /// <summary>
    /// Interaction logic for CalendarMonthPanel.xaml
    /// </summary>
    public partial class CalendarMonthPanel : UserControl
    {
        private CalendarMonthItem[] monthItems = new CalendarMonthItem[42];
        private Popup calPopup;
        private CalendarPopupMenu popupMenu;
		private CalendarMonthItem selectedItem = new CalendarMonthItem();
		private int thisMonth;
		private CalendarWindow ancestorWindow;

        public CalendarMonthPanel()
        {
            InitializeComponent();

            MonthItemGrid.Children.Clear();

            for (int i = 0; i < monthItems.Length; i++)
            {
                monthItems[i] = new CalendarMonthItem();
				monthItems[i].MouseLeftButtonDown += MonthItem_MouseLeftButtonDown;
                MonthItemGrid.Children.Add(monthItems[i]);
            } // end for

            calPopup = new Popup();
            popupMenu = new CalendarPopupMenu();
            calPopup.Child = popupMenu;
            calPopup.Placement = System.Windows.Controls.Primitives.PlacementMode.MousePoint;

        }

		public CalendarWindow AncestorWindow
		{
			get { return ancestorWindow; }
			set { ancestorWindow = value; }
		}

        public void prtMonthCal(DateTime iMonth)
        {
            DateTime firstDay = new DateTime(iMonth.Year, iMonth.Month, 1);
			int fIndex = -(int)firstDay.DayOfWeek;

			selectedItem = new CalendarMonthItem();
			thisMonth = iMonth.Month;

            if (fIndex == 0) fIndex = -7;

            firstDay = firstDay.AddDays(fIndex);

            for (int i = 0; i < monthItems.Length; i++)
            {
				if ((i == 0) || (firstDay.Day == 1)) // first day panel of months
					monthItems[i].lblDay.Content = firstDay.Month.ToString() + "/" + firstDay.Day.ToString();
				else
					monthItems[i].lblDay.Content = firstDay.Day;

				if (firstDay == AncestorWindow.selectDate)
					selectedItem = monthItems[i];

				monthItems[i].thisDate = firstDay;
				paintItem(monthItems[i]);
                firstDay = firstDay.AddDays(1);
            } // end for
        }

		private void paintItem(CalendarMonthItem monthItem)
		{
			// back color
			if (monthItem.thisDate == selectedItem.thisDate) // selected day
				modifyPanelColor(monthItem, Color.FromRgb(255, 200, 200));
			else if (monthItem.thisDate == DateTime.Today) // today
				modifyPanelColor(monthItem, Color.FromRgb(255, 255, 200));
			else if (monthItem.thisDate.Month == thisMonth) // this month
				modifyPanelColor(monthItem, Colors.White);
			else // other months
				modifyPanelColor(monthItem, Color.FromRgb(200, 200, 200));

			// forecolor
			if (monthItem.thisDate.Month == thisMonth) // this month
				modifyForeColor(monthItem, Colors.Black);
			else // other months
				modifyForeColor(monthItem, Colors.Gray);

			// style
			if (monthItem.thisDate == selectedItem.thisDate) // selected day
				monthItem.lblDay.FontWeight = FontWeights.Bold;
			else
				monthItem.lblDay.FontWeight = FontWeights.Normal;
		}

		private void modifyPanelColor(CalendarMonthItem monthItem, Color color)
		{
			monthItem.Background = new SolidColorBrush(color);
			monthItem.listBoxEvent.Background = new SolidColorBrush(color);
		}

		private void modifyForeColor(CalendarMonthItem monthItem, Color color)
		{
			monthItem.lblDay.Foreground = new SolidColorBrush(color);
		}

		private MouseButtonEventHandler MonthItem_MouseLeftButtonDown(int i, EventArgs eventArgs)
		{
			throw new NotImplementedException();
		} // end

		private void MonthItem_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
		{
			CalendarMonthItem thisItem = e.Source as CalendarMonthItem;
			CalendarMonthItem selItem = selectedItem;
			selectedItem = thisItem;
			paintItem(selItem); // recovery original color
			paintItem(thisItem);
			AncestorWindow.RefreshDay(thisItem.thisDate);
		}

        private void UniformGrid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (e.ClickCount == 2)
            {
                ListBoxItem listBoxItem = new ListBoxItem();
                listBoxItem.MouseDoubleClick += new MouseButtonEventHandler(listBoxItem_MouseDoubleClick);
                popupMenu.Bind_Item(listBoxItem);

                listBoxItem.Content = "제목 없음";
                listBoxItem.Foreground = new SolidColorBrush(Colors.Red);

                CalendarMonthItem monthItem = e.Source as CalendarMonthItem;
                monthItem.listBoxEvent.Items.Add(listBoxItem);
                monthItem.listBoxEvent.SelectedItem = listBoxItem;

                calPopup.IsOpen = true;
            }
            else
            {
                calPopup.IsOpen = false;
                popupMenu.Clear_Content();
            }
        }

        void listBoxItem_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            ListBoxItem listBoxItem = sender as ListBoxItem;
            popupMenu.Bind_Item(listBoxItem);
            calPopup.IsOpen = true;
        }

    } // end class
}